<?php
namespace app\car\model;
use app\BaseModel;
use think\facade\Db;



class CarList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_motor_car_list';


    protected $append = [

        'setting',

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 16:09
     * @功能说明:转换格式
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 16:09
     * @功能说明:转换格式
     */
    public function getTrueImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-24 16:09
     * @功能说明:转换格式
     */
    public function getCityCodeAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-24 16:09
     * @功能说明:转换格式
     */
    public function getAscriptionCityCodeAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-22 15:28
     * @功能说明:获取配置信息
     */
    public function getSettingAttr($value,$data){

        if(!empty($data['id'])){

            $text_model = new CarSettingText();

            $dis = [

                'car_id' => $data['id']
            ];

            $list = $text_model->where($dis)->column('set_id');

            return array_values($list);

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['info'])){

            $info = $data['info'];

            unset($data['info']);
        }

        if(isset($data['setting'])){

            $setting = $data['setting'];

            unset($data['setting']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['ascription_city_code'])){

            $data['ascription_city_code'] = implode(',',$data['ascription_city_code']);
        }



        if(!empty($data['imgs'])){

            $data['city_code'] = implode(',',$data['city_code']);
        }

        if(!empty($data['true_imgs'])){

            $data['true_imgs'] = implode(',',$data['true_imgs']);

        }else{

            $data['true_imgs'] = '';

        }

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        if(isset($info)){

            $this->updateSome($id,$data['uniacid'],$info);
        }

        if(isset($setting)){

            $this->addSetting($id,$data['uniacid'],$setting);
        }
        return $id;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-19 17:48
     * @功能说明:更新
     */
    public function updateSome($id,$uniacid,$info){

        if(!empty($info)){

            $car_info_model = new CarInfo();
            //兼容
           // $car_info_model->where(['type_key'=>'12喇叭'])->delete();

            foreach ($info as $value){

                $dis = [

                    'car_id'  => $id,

                    'key'     => $value['key'],

                    'type_key'=> isset($value['type_key'])?$value['type_key']:'',

                ];

                $find = $car_info_model->dataInfo($dis);

                $insert = [

                    'car_id'   => $id,

                    'key'      => $value['key'],

                    'type_key' => isset($value['type_key'])?$value['type_key']:'',

                    'type_ind' => isset($value['type_ind'])?$value['type_ind']:'',

                    'input_val' => isset($value['input_val'])?$value['input_val']:0,

                    'title_key'=> $value['title_key'],

                    'uniacid'  => $uniacid,

                    'text'     => $value['text'],

                    'value'    => $value['value'],

                    'is_int'   => $value['is_int'],

                ];

                if(!empty($find)){

                    $car_info_model->dataUpdate($dis,$insert);

                }else{

                    $car_info_model->insert($insert);

                }

            }

        }


        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 14:24
     * @功能说明:添加配置
     */
    public function addSetting($id,$uniacid,$setting){
        //添加汽车配置
        $text_model = new CarSettingText();

        $text_model->where(['car_id'=>$id])->delete();

        if(!empty($setting)){

            foreach ($setting as $key =>  $value){

                $insert[$key] = [

                    'uniacid' => $uniacid,

                    'car_id'  => $id,

                    'set_id'  => $value
                ];

            }

            $text_model->saveAll($insert);
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['info'])){

            $info = $data['info'];

            unset($data['info']);
        }

        if(isset($data['setting'])){

            $setting = $data['setting'];

            unset($data['setting']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['ascription_city_code'])){

            $data['ascription_city_code'] = implode(',',$data['ascription_city_code']);
        }

        if(!empty($data['imgs'])){

            $data['city_code'] = implode(',',$data['city_code']);
        }

        if(!empty($data['true_imgs'])){

            $data['true_imgs'] = implode(',',$data['true_imgs']);

        }else{

            $data['true_imgs'] = '';

        }

        $res = $this->where($dis)->update($data);

        if(isset($info)){

            $this->updateSome($data['id'],$data['uniacid'],$info);
        }

        if(isset($setting)){

            $this->addSetting($data['id'],$data['uniacid'],$setting);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function indexDataList($dis,$sort,$page=10){

        $top  = $this->getSort($sort);

        $data = $this->where($dis)->order($top)->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-03 10:28
     * @功能说明:获取排序规则
     */
    public function getSort($sort){

        switch ($sort){

            case 1:

                $text = 'view_count desc,id desc';

                break;

            case 2:

                $text = 'id desc';

                break;

            case 3:

                $text = 'price,id desc';

                break;

            case 4:

                $text = 'price desc,id desc';

                break;

            case 5:

                $text = 'mileage,id desc';

                break;

            case 6:

                $text = 'licensing_time desc,id desc';

                break;

            default:

                $text = 'id desc';

                break;


        }

        return $text;



    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 14:18
     * @功能说明:后台列表
     */
    public function adminDataList($dis,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_motor_car_cate b','a.cate_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.title as cate_name')
                ->group('a.id')
                ->order('top desc,id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }







}